# -*- coding: UTF-8 -*-
import re
import urllib
import urllib2
import json

# 取首页源码
def getHtml(url):
    page = urllib.urlopen(url)
    html = page.read()
    page.close()
    return html
# 获取大分类
def getType(html):
    reg = 'data-target="#collapse_(.*?)">(.*?)</a>'
    urllist = re.findall(reg, html)
    for url in urllist:
        print 'id: ' + url[0] + ', name: ' + url[1]
        getIndex(url[0])
def getIndex(id):
    data = {'id':id}
    f = urllib2.urlopen(
        url = 'http://itellyou.cn/Category/Index',
        data = urllib.urlencode(data)
    )
    value = json.loads(f.read())
    for js in value:
        print '--id: ' + js['id'] + ', name: ' + js['name']
        getLang(js['id'])
def getLang(id):
    data = {'id': id}
    f = urllib2.urlopen(
        url = 'http://itellyou.cn/Category/GetLang',
        data = urllib.urlencode(data)
    )
    value = json.loads(f.read())
    for aaaa in value['result']:
        print '----id: ' + aaaa['id'] + ', lang: ' + aaaa['lang']
        getList(id, aaaa['id'])
def getList(id, lang):
    data = {'id':id, 'lang':lang, 'filter':'true'}
    f = urllib2.urlopen(
        url = 'http://itellyou.cn/Category/GetList',
        data = urllib.urlencode(data)
    )
    value = json.loads(f.read())
    for aaaa in value['result']:
        print '------id: ' + aaaa['id'] + ', name: ' + aaaa['name'] + ', post: ' + aaaa['post'] + ', url: ' + aaaa['url']
        getProduct(aaaa['id'])
def getProduct(id):
    data = {'id':id}
    f = urllib2.urlopen(
        url = 'http://itellyou.cn/Category/GetProduct',
        data = urllib.urlencode(data)
    )
    value = json.loads(f.read())
    print ' FileName: ' + value['result']['FileName']
    print ' size: ' + value['result']['size']
    print' PostDate: ' + value['result']['PostDateString']
    print ' SHA1: ' + value['result']['SHA1']
    print ' DownLoad: ' + value['result']['DownLoad']

if __name__ == '__main__':
    html = getHtml('http://itellyou.cn/')
    getType(html)
